﻿using STMS.Models.DModels;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace STMS.DAL
{
    public class DZPDDAL : BaseDAL<HistorInfo>
    {
        /// <summary>
        /// 条件查询电子皮带核查信息列表
        /// </summary>
        /// <param name="keywords">查询关键字</param>
        /// <param name="isDeleted">是否已删除</param>
        /// <returns></returns>
        public List<HistorInfo> GetHistorInfos(string keywords, string Station)
        {
            string cols = "R_ID,R_C_ID,R_TIME,R_STAND,R_RECORD,R_RESULT,R_PERSON,R_OP";
            string strWhere = "R_C_ID = 1";
          
            if ((!string.IsNullOrEmpty(keywords)) && (!string.IsNullOrEmpty(Station)))
            {
                strWhere += "and (R_OP like @Station)";
                strWhere += "and (CONVERT(varchar,R_TIME,120) like @keywords)";
                //strWhere += " and (CONVERT(varchar(7),DZPDCheckTime,120) =  CONVERT(varchar(7),GETDATE(),120)) ";
                //SqlParameter para = new SqlParameter("@keywords", $"%{keywords}%");
                //SqlParameter para1 = new SqlParameter("@keywords", $"%{keywords}%");
                SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@keywords", $"%{keywords}%"), new SqlParameter("@Station", $"%{Station}%") };
                return GetModelList(strWhere, cols, paras);
            }
            else if ((string.IsNullOrEmpty(keywords)) && (!string.IsNullOrEmpty(Station)))
            {
                strWhere += "and(R_TIME between dateadd(DAY,-365,GETDATE()) and GETDATE())";
                strWhere += "and (R_OP like @Station)";
                SqlParameter para = new SqlParameter("@Station", $"%{Station}%");
                return GetModelList(strWhere, cols, para);
            }
            else if ((!string.IsNullOrEmpty(keywords)) && (string.IsNullOrEmpty(Station)))
            {
                strWhere += "and((CONVERT(varchar,R_TIME,120) like @keywords))";
                SqlParameter para = new SqlParameter("@keywords", $"%{keywords}%");
                return GetModelList(strWhere, cols, para);
            }
            strWhere += "and(R_TIME between dateadd(DAY,-365,GETDATE()) and GETDATE())";
            return GetModelList(strWhere, cols);
        }
        /// <summary>
        /// 获取指定行核查结果信息
        /// </summary>
        /// <param name="dzpdCheckId"></param>
        /// <returns></returns>
        public HistorInfo GetInfo(int dzpdCheckId)
        {
            string cols = CreateSql.GetColsString<HistorInfo>("R_ID,R_C_ID,R_OP");
            return GetById(dzpdCheckId, cols);
        }
        /// <summary>
        /// 添加核查结果信息
        /// </summary>
        /// <param name="dzpdInfo"></param>
        /// <param name="Isreturn">0代表不返回</param>
        /// <returns></returns>
        public int AddDZPDInfos(HistorInfo dzpdInfo, int Isreturn)
        {
            string cols = "R_C_ID,R_STAND,R_RECORD,R_RESULT,R_PERSON,R_OP";
            return Add(dzpdInfo, cols, Isreturn);
        }
        /// <summary>
        /// 修改核查结果信息
        /// </summary>
        /// <param name="dzpdInfo"></param>
        /// <returns></returns>
        public bool UpdateDZPDInfos(HistorInfo dzpdInfo)
        {
            string cols = "R_ID,R_STAND,R_RECORD,R_RESULT,R_PERSON";
            return Update(dzpdInfo, cols);
        }
    }
}